package com.hm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.reggie.common.R;
import com.hm.reggie.entity.Employee;
import com.hm.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /***
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //1、把页面提交的密码password进行加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的username到数据库中去查询是否存在该用户
        //wrapper.eq(实体类::get字段，条件值)=where 实体类.字段==条件值
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(lambdaQueryWrapper);

        //3、判断查询条件是否为空
        if (one == null) {
            return R.error("用户不存在！");
        }

        //4、比对密码
        if (!one.getPassword().equals(password)){
            return R.error("密码错误！");
        }

        //5、检查账号的状态是否可用
        if (one.getStatus()==0){
            return R.error("账号被锁定！");
        }

        //6、把用户信息放到session
        request.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }

    /**
     * 用户退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request){
        //request.getSession().setAttribute("employee","");
        request.getSession().removeAttribute("employee");
        return  R.success(null);
    }

    /***
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工信息：{}",employee.toString());
        //设置密码
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        //employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));

        employeeService.save(employee);
        return  R.success("新增员工成功");
    }


    /**
     * 员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page,int pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        //构造分页构造器
        Page<Employee> pageInfo =new Page<>(page,pageSize);

        //条件封装,添加条件
        LambdaQueryWrapper<Employee> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查村
        employeeService.page(pageInfo,lambdaQueryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改用户信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<Employee> update(HttpServletRequest request, @RequestBody Employee employee){
        log.info("------{修改用户状态}------");
        log.info(employee.toString());
        //employee.setUpdateUser(Long.valueOf(request.getSession().getAttribute("employee").toString()));
        //employee.setUpdateTime(LocalDateTime.now());
        boolean b = employeeService.saveOrUpdate(employee);
        log.info("保存/修改结果：{}",b?"成功":"失败");

        return R.success(employee);
    }


    /**
     * 根据id获取到对象
     * 请求方法：get，参数携带方式：/id
     * http://localhost:8080/employee/1640006562263724034
     * 获取到参数的方法：@GetMapping("/{id}")    @PathVariable Long id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("------根据id获取员工信息------");
        Employee employee = employeeService.getById(id);
        return employee!=null?R.success(employee):R.error("没有查询到该员工信息");
    }


}
